package ch8recursion;

import java.util.Arrays;


public class Ex8 {
	private static final int NUM_QUEENS = 4;
	static int columnForRow[] = new int[NUM_QUEENS];

	public static void main(String[] argv) {
		PlaceQueen(0);
	}

	static boolean check(int row) {
		int column = columnForRow[row];
		for (int i=0; i<row; i++) {
			int diff = Math.abs(columnForRow[i] - column);
			if (diff == 0 || diff == row - i) {
				return false;
			}
		}
		return true;
	}

	static void PlaceQueen(int row) {
		if (row == NUM_QUEENS) {
			printBoard();
		}
		else {
			for (int i=0; i<NUM_QUEENS; i++) {
				columnForRow[row] = i;
				if (check(row)) {
					PlaceQueen(row+1);
				}
			}
		}
		
	}

	static void printBoard() {
		for (int i=0; i<NUM_QUEENS; i++) {
			int column = columnForRow[i];
			for (int j=0; j<NUM_QUEENS; j++) {
				if (j == column) {
					System.out.print("Q");
				}
				else {
					System.out.print("*");					
				}
			}
			System.out.println();
		}
		System.out.println();
	}
}
